#include "BondDetailCfets.h"
#include "DetailQuoteTable.h"
#include "BondDetailData.h"
#include "BondDetailPrcInter.h"
#include <QStylePainter>
#include <QVBoxLayout>
#include <widgets/table/STable.h>
#include <widgets/table/STableColumnSet.h>
#include <QTableView>
#include <QHeaderView>
#include <widgets/SComboBox.h>
#include <core/StyleMgr.h>

BondDetailCFETS::BondDetailCFETS(QWidget* p)
    : QWidget(p)
{

}

BondDetailCFETS::~BondDetailCFETS()
{
}

void BondDetailCFETS::paintEvent(QPaintEvent* event)
{
    QRect r = rect();
    r.setBottom(m_bestTable->frameGeometry().top());
    QStylePainter painter(this);
    painter.fillRect(r, StyleMgr::instance().color("BasicPlate"));
}

void BondDetailCFETS::setDetailData(QSharedPointer<DetailData> data)
{
    m_data=data;

    QVBoxLayout* l = new QVBoxLayout();
    l->setContentsMargins(0, 4, 0, 0);
    l->setSpacing(4);
    setLayout(l);

    {
        QHBoxLayout* titleLayout = new QHBoxLayout();
        titleLayout->setContentsMargins(0, 0, 4, 0);
        titleLayout->setSpacing(4);
        l->addLayout(titleLayout);

        titleLayout->addStretch();
        titleLayout->addWidget(new QLabel(tr("清算速度"), this));

        SComboBox *m_cbClearSpeed = new SComboBox(this);
        titleLayout->addWidget(m_cbClearSpeed);
        m_cbClearSpeed->setFixedSize(96, 24);
        QStringList speeds;
        speeds << tr("不限") << tr("T+0") << tr("T+1");// << tr("远期") << tr("非远期");
        m_cbClearSpeed->addItems(speeds);
        //connect(m_cbClearSpeed, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &BrokerBestPrice::onClearSpeedChanged);
    }

    {
        m_bestTable = new SVirtualTable(this);
        m_bestTable->setFixedHeight(60);


        l->addWidget(m_bestTable);
        initBestPriceList();
    }

    m_table = new DetailQuoteTable(this, m_data, DETAILOTHERTYPE_CFETS);
    l->addWidget(m_table);

    BondDetailPrcInter* m_prcInter = new BondDetailPrcInter(this);
    m_prcInter->setDetailData(m_data);
    m_prcInter->setFixedHeight(40);

    l->addWidget(m_prcInter);


    connect(m_data.get(), &DetailData::bondChanged, this, &BondDetailCFETS::onBondChanged);
}

void BondDetailCFETS::initBestPriceList()
{
    DefaultTableModel* m = new DefaultTableModel(this);
    m->setTable(m_bestTable);
    m_bestTable->table()->setModel(m);

    m_tableSet = new STableColumnSet(m_bestTable,"BrokerBestPrice", this);
    m_tableSet->setTableWidthFixed(true);
    BEGIN_COLUMN;
    ADD_COLUMN(0, m_tableSet, "类型", 0, 40, 12, -1, false, (Qt::Alignment)(Qt::AlignVCenter | Qt::AlignLeft), kFixed, "");
    ADD_COLUMN(1, m_tableSet, "买方", 0, -1, 12, 0.25, false, (Qt::Alignment)(Qt::AlignVCenter | Qt::AlignLeft), kAuto, "");
    ADD_COLUMN(2, m_tableSet, "Vol.Bid", 0, -1, 12, 0.25, false, (Qt::Alignment)(Qt::AlignVCenter | Qt::AlignRight), kAuto, "");
    ADD_COLUMN(3, m_tableSet, "Bid", 0, -1, 12, 0.25, false, (Qt::Alignment)(Qt::AlignVCenter | Qt::AlignRight), kAuto, "");
    ADD_COLUMN(4, m_tableSet, "Ofr", 0, -1, 12, 0.25, false, (Qt::Alignment)(Qt::AlignVCenter | Qt::AlignRight), kAuto, "");
    ADD_COLUMN(5, m_tableSet, "Vol.Ofr", 0, -1, 12, 0.25, false, (Qt::Alignment)(Qt::AlignVCenter | Qt::AlignRight), kAuto, "");
    ADD_COLUMN(6, m_tableSet, "卖方", 0, -1, 12, 0.25, false, (Qt::Alignment)(Qt::AlignVCenter | Qt::AlignLeft), kAuto, "");
    ADD_COLUMN(7, m_tableSet, tr("更新时间"), 0, 60, 12, -1, false, (Qt::Alignment)(Qt::AlignVCenter | Qt::AlignRight), kFixed, "");
    m_tableSet->apply();

    m_bestTable->table()->horizontalHeader()->show();
}

void BondDetailCFETS::onBondChanged()
{
	m_data->reqCfetsMarketStream(0);
	m_table->update();
}
